<template>
  <el-dialog
    :visible.sync="isShow"
    title="标签编辑"
    width="400px"
    top="60px"
    :close-on-click-modal="false"
  >
    <el-form ref="form" :model="fields" :rules="rules" label-width="80px">
      <el-form-item label="标签名称" prop="label">
        <el-input
          v-model.trim="fields.label"
          autocomplete="off"
          size="small"
          placeholder="请输入标签名称"
          maxlength="64"
        />
      </el-form-item>
      <el-form-item label="备注:" prop="remark">
        <el-input
          autocomplete="off"
          maxlength="200"
          placeholder="请输入备注"
          rows="1"
          show-word-limit
          size="small"
          type="textarea"
          v-model.trim="fields.remark"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :loading="loading" size="mini" @click="closePage"
        >取消</el-button
      >
      <el-button
        :loading="loading"
        type="primary"
        size="mini"
        @click="onOkBtnClick"
        >保存</el-button
      >
    </div>
  </el-dialog>
</template>

<script>
import { saveOrUpdate, get } from '@/api/label'
export default {
  props: {
    value: {
      type: Object,
      default: () => Object.create({}),
    },
  },
  data() {
    return {
      isShow: true,
      fields: {},
      parents: [],
      rules: {
        label: [{ required: true, message: '请输入标签名称', trigger: 'blur' }],
      },
    }
  },
  watch: {
    isShow: function() {
      this.$emit('input', null)
    },
  },
  created() {
    this.get(this.value.id || null)
  },
  methods: {
    onOkBtnClick() {
      this.submitForm()
    },
    submitForm() {
      this.$refs['form'].validate((valid) => {
        valid &&
          saveOrUpdate(this.fields)
            .then(() => {
              this.isShow = false
              this.$parent.onSearchBtnClick()
            })
            .catch(() => {})
      })
    },
    resetForm() {
      this.$refs['form'].resetFields()
    },
    async get(id) {
      const res = await get(id)
      if (!res.multipleDb) {
        res.multipleDb = 'false'
      }
      this.fields = res
    },
    closePage() {
      this.isShow = false
    },
  },
}
</script>
